package com.xuecheng.content.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.BindTeachplanMediaDto;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.domain.TeachplanMedia;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanMediaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.media.api.MediaClient;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-12-03
 */
@Service
public class TeachplanMediaServiceImpl extends ServiceImpl<TeachplanMediaMapper, TeachplanMedia> implements TeachplanMediaService {
    @Autowired
    private CourseBaseService courseBaseService;
    @Autowired
    private TeachplanService teachplanService;
    @Autowired
    private MediaClient mediaClient;

    //计划绑定媒资
    @Override
    public ResponseResult association(BindTeachplanMediaDto dto) {
    //参数校验 前端传递参数是否为空
        if (ObjectUtil.isEmpty(dto.getMediaId())||ObjectUtil.isEmpty(dto.getTeachplanId())) {
            throw  new BusinessException(ErrorCode.DATAERROR);
        }
    //判断课程状态 是否已发布 已提交 已审核
        // 根据service查询课程计划
        Teachplan teachplan = teachplanService.getById(dto.getTeachplanId());
        //根据课程计划里的课程id查询课程
        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        if (courseBase.getAuditStatus().equals(CourseConstants.AUDIT_COMMIT_STATUS)||
            courseBase.getAuditStatus().equals(CourseConstants.AUDIT_PUBLISHED_STATUS)||
            courseBase.getAuditStatus().equals(CourseConstants.AUDIT_PASTED_STATUS)) {
            throw new BusinessException(ErrorCode.UPDATEEERROR);

        }
        //通过feign远程调用查询出媒资对象
        ResponseResult<MediaVo> mediaVoResponseResult = mediaClient.findById(dto.getMediaId());
        //判断是否调用成功
        if (mediaVoResponseResult.getCode()!=0) {
            throw new BusinessException(ErrorCode.ADDEERROR);
        }
        //取出媒资对象
        MediaVo mediaVo = mediaVoResponseResult.getData();
        //设置封装媒资课程绑定表
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setTeachplanId(teachplan.getId());//课程计划id
        teachplanMedia.setMediaId(dto.getMediaId());//媒资id
        teachplanMedia.setChangePeople(AuthInfoHolder.getUserId().toString());//创建人
        teachplanMedia.setCourseId(courseBase.getId());//课程id
        teachplanMedia.setCreateDate(new Date());
        teachplanMedia.setMediaFilename(mediaVo.getFilename());
        //将媒资绑定对象放入数据库
        this.save(teachplanMedia);
        //返回ok
        return ResponseResult.okResult();
    }
    //媒资解绑
    @Override
    public ResponseResult deleteAssociation(Long id) {
        //判断条件是否为空
        if (ObjectUtil.isEmpty(id)) {
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        // 根据service查询媒资绑定对象
        TeachplanMedia teachplanMedia = this.getById(id);
        //根据课程计划媒资表里的课程id查询课程
        //判断课程状态 是否已发布 已提交 已审核
        CourseBase courseBase = courseBaseService.getById(teachplanMedia.getCourseId());
        if (courseBase.getAuditStatus().equals(CourseConstants.AUDIT_COMMIT_STATUS)||
                courseBase.getAuditStatus().equals(CourseConstants.AUDIT_PUBLISHED_STATUS)||
                courseBase.getAuditStatus().equals(CourseConstants.AUDIT_PASTED_STATUS)) {
            throw new BusinessException(ErrorCode.UPDATEEERROR);

        }
        //通过id对媒资课程表中数据进行删除
        this.removeById(id);
        //返回ok
        return ResponseResult.okResult();
    }
}
